package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrMedicineDAO;
import com.trkj.dao.jpa.IYzrPharmacyDAO;
import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.IYzrSupplierDAO;
import com.trkj.dao.jpa.IYzrTbdepartmentDAO;
import com.trkj.dao.jpa.IYzrTbpostDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbpost;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrMedicine;
import com.trkj.pojos.YzrPharmacy;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.pojos.YzrSupplier;
import com.trkj.vo.YzrPharmacyVo;
import com.trkj.vo.YzrStaffVo;
import com.trkj.vo.YzrStorehouseVo;


@Service
@Transactional
public class YzrEntityService {
	
	@Autowired
	private IYzrMedicineDAO yzrMedicineDAO;
	@Autowired
	private IYzrPharmacyDAO yzrPharmacyDAO;
	@Autowired
	private IYzrTbstaffDAO yzrTbstaffDAO;
	@Autowired
	private IYzrTbdepartmentDAO yzrTbdepartmentDAO;
	@Autowired
	private IYzrTbpostDAO yzrTbpostDAO;
	@Autowired
	private IYzrSupplierDAO yzrSupplierDAO;
	@Autowired
	private IYzrStorehouseDAO yzrStorehouseDAO;
	
	
	/**
	 * 根据名称模糊查询药品
	 * @param name
	 * @return
	 */
	public List<YzrMedicine> queryMedicineByName(String name){
		return yzrMedicineDAO.findByMedNameLike(name);
	}
	
	/**
	 * 根据类型模糊查询药品
	 * @param kind
	 * @return
	 */
	public List<YzrMedicine> queryMedicineByKind(String kind){
		return yzrMedicineDAO.findByMedKindLike(kind);
	}
	/**
	 * 批量删除药品
	 * @param list
	 */
	public void delMedicine(String[] list){
		for(String medId: list){
			yzrMedicineDAO.delete(medId);
		}
	}
	/**
	 * 根据编号查找药品
	 * @param medId
	 * @return
	 */
	public YzrMedicine queryMedicineById(String medId){
		return yzrMedicineDAO.findOne(medId);
	}
	
	/**
	 * 编辑药品
	 * @param medicine
	 */
	public void editMedicine(YzrMedicine medicine){
		yzrMedicineDAO.save(medicine);
	}
	/**
	 * 新增药品
	 * @param medicine
	 */
	public void addMedicine(YzrMedicine  medicine){
		// 获取年份
		String year = getNowDate();
		//拼接
		String name = "YP-";	
		name = name + year; 			
		String n = yzrMedicineDAO.findID(name);
		//System.out.println("从数据库中读取的最大编号："+n);
		String id = this.getId(name, n);
		
		medicine.setMedId(id);
		
		yzrMedicineDAO.save(medicine);
	}
	/**
	 * 分页查询药品
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryAllMedicine(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrMedicine> list = yzrMedicineDAO.findAll(pr);
		long total = yzrMedicineDAO.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	public List<YzrMedicine> queryAllMedicine2(){
		List<YzrMedicine> list = yzrMedicineDAO.findAllMedicine();
		return list;
	}
	
/***************************************************************************************/
	
	/**
	 * 根据名称模糊查询仓库
	 * @param storeName
	 * @return
	 */
	public List<YzrStorehouseVo> queryStorehouseByName(String storeName){
		List<YzrStorehouse> list = yzrStorehouseDAO.findByStoreNameLike(storeName);
		return storehouseListToVo(list);
	}
	
	/**
	 * 批量删除仓库
	 */
	public void delStorehouse(String[] list){
		for(String sId:list){
			yzrStorehouseDAO.delete(sId);
		}
	}
	
	/**
	 * 修改仓库信息
	 * @param store
	 */
	public void editStorehouse(YzrStorehouse store){
		yzrStorehouseDAO.save(store);
	}
	
	/**
	 * 根据id查找仓库
	 * @param sId
	 * @return
	 */
	public YzrStorehouseVo queryStorehouseById(String sId){
		YzrStorehouse store = yzrStorehouseDAO.findOne(sId);
		YzrStorehouseVo storeVo = storehousePoToVo(store);
		return storeVo;
	}
	
	/**
	 * 添加仓库
	 * @param store
	 */
	public void addStorehouse(YzrStorehouse store){
		// 获取年份
		String year = getNowDate();
		//拼接
		String name = "CK-";	
		name = name + year; 			
		String n = yzrStorehouseDAO.findID(name);
		//System.out.println("从数据库中读取的最大编号："+n);
		String id = this.getId(name, n);
				
		store.setStoreId(id);
				
		yzrStorehouseDAO.save(store);		
	}
	
	/**
	 * 分页查询仓库信息
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryAllStorehouse(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrStorehouse> list = yzrStorehouseDAO.findAll(pr);
		long total = yzrStorehouseDAO.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", storehouseListToVo(list));
		return map;
	}
	
	/**
	 * 将YzrStorehouse的集合转换成YzrStorehouseVo的List集合
	 * @param list
	 * @return
	 */
	private List<YzrStorehouseVo> storehouseListToVo(List<YzrStorehouse> list){
		if(list == null) return null;
		List<YzrStorehouseVo> listVo = new ArrayList<>();
		
		for(YzrStorehouse store: list){
			YzrStorehouseVo storeVo = storehousePoToVo(store);
			listVo.add(storeVo);
		}
		return listVo;
	}
	/**
	 * 仓库属性复制
	 * @param store
	 * @return
	 */
	public YzrStorehouseVo storehousePoToVo(YzrStorehouse store){
		YzrStorehouseVo storeVo = new YzrStorehouseVo();
		storeVo.setStoreId(store.getStoreId());
		storeVo.setStoreName(store.getStoreName());
		storeVo.setStoreKind(store.getStoreKind());
		storeVo.setStoreChargeperson(store.getStoreChargeperson());
		storeVo.setStoreArea(store.getStoreArea());
		storeVo.setStoreAddress(store.getStoreAddress());
		
		//获取负责人id
		BigDecimal fzr = store.getStoreChargeperson();	
		if(fzr != null){		
			long sId = fzr.longValue();		
			Tbstaff staff = yzrTbstaffDAO.findOne(sId);		
			storeVo.setStaffNo(staff==null?0:staff.getStaffNo());;
			storeVo.setStaffName(staff==null?null:staff.getStaffName());
		}
		return storeVo;
	}
	
	
	
	
	
/***************************************************************************************/	
	
	
	/**
	 * 根据名称模糊查询供应商
	 * @param name
	 * @return
	 */
	public List<YzrSupplier> querySupplierByName(String name){
		return yzrSupplierDAO.findBySupNameLike(name);
	}
	
	/**
	 * 根据类型模糊查询供应商
	 * @param kind
	 * @return
	 */
	public List<YzrSupplier> querySupplierByKind(String kind){
		return yzrSupplierDAO.findBySupKindLike(kind);
	}
	/**
	 * 批量删除供应商
	 * @param list
	 */
	public void delSupplier(String[] list){
		for(String sup: list){
			yzrSupplierDAO.delete(sup);
		}
	}
	/**
	 * 根据编号查找供应商
	 * @param supId
	 * @return
	 */
	public YzrSupplier querySupplierById(String supId){
		return yzrSupplierDAO.findOne(supId);
	}
	
	/**
	 * 编辑供应商
	 * @param supplier
	 */
	public void edittSupplier(YzrSupplier  supplier){
		yzrSupplierDAO.save(supplier);
	}
	/**
	 * 新增供应商
	 * @param supplier
	 */
	public void addSupplier(YzrSupplier  supplier){
		// 获取年份
		String year = getNowDate();
		//拼接
		String name = "GY-";	
		name = name + year; 			
		String n = yzrSupplierDAO.findID(name);
		//System.out.println("从数据库中读取的最大编号："+n);
		String id = this.getId(name, n);
		
		supplier.setSupId(id);
		
		yzrSupplierDAO.save(supplier);
	}
	/**
	 * 分页查询供应商
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryAllSupplier(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrSupplier> list = yzrSupplierDAO.findAll(pr);
		long total = yzrSupplierDAO.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 分页查询供应商
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryAllSupplier2(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrSupplier> list = yzrSupplierDAO.findAll2(pr);
		long total = yzrSupplierDAO.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}

	
/***************************************************************************************/
	
	/**
	 * 查询所有员工(包含员工所在科室的名称)
	 * @return
	 */
	public List<YzrStaffVo> queryAllStaff(){
		List<Tbstaff> staffList = (List<Tbstaff>) yzrTbstaffDAO.findAll();
		return staffListToVo(staffList);
	}
	
	public List<YzrStaffVo> queryAllStaff2(){
		List<Tbstaff> staffList = (List<Tbstaff>) yzrTbstaffDAO.findAllStaff();
		return staffListToVo(staffList);
	}
	
	/**
	 * 将Tbstaff的集合转换成YzrStaffVo的List集合
	 * @param list
	 * @return
	 */
	private List<YzrStaffVo> staffListToVo(List<Tbstaff> list){
		if(list == null) return null;
		List<YzrStaffVo> listVo = new ArrayList<>();
		
		for(Tbstaff staff: list){
			YzrStaffVo staffVo = staffPoToVo(staff);
			listVo.add(staffVo);
		}
		return listVo;
	}
	
	/**
	 * 员工属性复制
	 * @param staff
	 * @return
	 */
	public YzrStaffVo staffPoToVo(Tbstaff staff){
		YzrStaffVo staffVo = new YzrStaffVo();
		staffVo.setStaffNo(staff.getStaffNo());
		staffVo.setStaffName(staff.getStaffName());
		
		//获取科室id
		BigDecimal dId = staff.getDepartmentNo();
		//获取职务id
		BigDecimal postId = staff.getStaffPostNo();
		
		
		if(dId != null){
			long sDid = dId.longValue();
			Tbdepartment department = yzrTbdepartmentDAO.findOne(sDid);			
			staffVo.setDepNo(department==null?0:department.getDepartmentNo());
			staffVo.setDepartmentName(department==null?null:department.getDepartmentName());
		}				
		if(postId != null){
			long sDid = postId.longValue();			
			Tbpost post = yzrTbpostDAO.findOne(sDid);		
			staffVo.setPostNo(post==null?0:post.getPostNo());
			staffVo.setPostName(post==null?null:post.getPostName());
		}		
		return staffVo;
	}
	

/***************************************************************************************/

	
	/**
	 * 批量删除药房
	 */
	public void delPharmacy(String[] list){
		for(String pId:list){
			yzrPharmacyDAO.delete(pId);
		}
	}
	
	/**
	 * 新增药房
	 * @param pharmacy
	 */
	public void addPharmacy(YzrPharmacy  pharmacy){
		// 获取年份
		String year = getNowDate();
		System.out.println("当前日期"+year);
		//拼接
		String name = "YF-";	
		name = name + year; 			
		String n = yzrPharmacyDAO.findID(name);
		String id = this.getId(name, n);
		
		pharmacy.setPharmacyId(id);
		
		yzrPharmacyDAO.save(pharmacy);
	}
	
	/**
	 * 格式化日期
	 * @return
	 */
	public static String getNowDate() {
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		  String dateString = formatter.format(currentTime);
		  return dateString;
	}
	/**
	 * 编辑药房信息
	 * @param pharmacy
	 */
	public void editPharmacy(YzrPharmacy pharmacy){
		yzrPharmacyDAO.save(pharmacy);
	}
	
	/**
	 * 根据id查找药房
	 * @param pId
	 * @return
	 */
	public YzrPharmacyVo queryPharmacyById(String pId){
		YzrPharmacy phar = yzrPharmacyDAO.findOne(pId);
		YzrPharmacyVo pharVo = pharmacyPoToVo(phar);
		return pharVo;
	}
	
	/**
	 * 根据名称模糊查询药房
	 * @param pharmacyName
	 * @return
	 */
	public List<YzrPharmacyVo> queryPharmacyByName(String pharmacyName){
		List<YzrPharmacy> list = yzrPharmacyDAO.findByPharmacyNameLike(pharmacyName);
		return pharmacyListToVo(list);
	}
	
	/**
	 * 查询所有的药房
	 * @return
	 */
	public Map<String, Object> queryAllPharmacy(int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<YzrPharmacy> list = yzrPharmacyDAO.findAll(pr);
		long total = yzrPharmacyDAO.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", pharmacyListToVo(list));
		return map;
	}
	
	/**
	 * 将YzrPharmacy的集合转换成YzrPharmacyVo的List集合
	 * @param list
	 * @return
	 */
	private List<YzrPharmacyVo> pharmacyListToVo(List<YzrPharmacy> list){
		if(list == null) return null;
		List<YzrPharmacyVo> listVo = new ArrayList<>();
		
		for(YzrPharmacy pharmacy: list){
			YzrPharmacyVo pharmacyVo = pharmacyPoToVo(pharmacy);
			listVo.add(pharmacyVo);
		}
		return listVo;
	}
	/**
	 * 药房属性复制
	 * @param pharmacy
	 * @return
	 */
	public YzrPharmacyVo pharmacyPoToVo(YzrPharmacy pharmacy){
		YzrPharmacyVo pharmacyVo = new YzrPharmacyVo();
		pharmacyVo.setPharmacyId(pharmacy.getPharmacyId());;
		pharmacyVo.setPharmacyName(pharmacy.getPharmacyName());
		pharmacyVo.setPharmacyAddress(pharmacy.getPharmacyAddress());
		
		//获取负责人id
		BigDecimal fzr = pharmacy.getPharmacyFuzeren();	
		if(fzr != null){		
			long sId = fzr.longValue();		
			//根据负责人id找到对应名字
			Tbstaff staff = yzrTbstaffDAO.findOne(sId);
		
			pharmacyVo.setStaffNo(staff==null?0:staff.getStaffNo());;
			pharmacyVo.setStaffName(staff==null?null:staff.getStaffName());
		}
		return pharmacyVo;
	}
	
/***************************************************************************************/
	
	
	/**
	 * 生成各种表的编号
	 * @param name
	 * @return
	 */
	public String getId(String name,String n){
		if(n==null){
			n=name+"00001";
		}else{
			long x=Integer.parseInt(n.substring (name.length())) +1;
			if(x<10){
				n=name+"0000"+x;
			}else if(x>=10 && x<100){
				n=name+"000"+x;
			}else if(x>=100 && x<1000){
				n=name+"00"+x;
			}else if(x>=1000 && x<10000){
				n=name+"0"+x;
			}else {
				n=name+x;
			}
		}
		System.out.println("生成的编号:"+n);
		return n;
	}
}
